﻿using System.Web.Mvc;

namespace CodeContrib.Web.Mvc.Html
{
	public static class ButtonExtensions
	{
		public static MvcHtmlString ResetButton(this HtmlHelper htmlHelper, string text)
		{
			return ResetButton(htmlHelper, text, null);
		}

		public static MvcHtmlString ResetButton(this HtmlHelper htmlHelper, string text, object htmlAttributes)
		{
			var resetButton = new TagBuilder("input");
			resetButton.MergeAttributes(htmlAttributes.ToHtmlAttributes());
			resetButton.Attributes["type"] = "reset";

			if (!string.IsNullOrEmpty(text))
			{
				resetButton.Attributes["value"] = text;
			}

			return MvcHtmlString.Create(resetButton.ToString());
		}

		public static MvcHtmlString SubmitButton(this HtmlHelper htmlHelper, string text)
		{
			return SubmitButton(htmlHelper, text, null, null);
		}

		public static MvcHtmlString SubmitButton(this HtmlHelper htmlHelper, string text, object htmlAttributes)
		{
			return SubmitButton(htmlHelper, text, null, htmlAttributes);
		}

		public static MvcHtmlString SubmitButton(this HtmlHelper htmlHelper, string text, string name)
		{
			return SubmitButton(htmlHelper, text, name, null);
		}

		public static MvcHtmlString SubmitButton(this HtmlHelper htmlHelper, string text, string name, object htmlAttributes)
		{
			var submitButton = new TagBuilder("input");
			submitButton.MergeAttributes(htmlAttributes.ToHtmlAttributes());

			if (!string.IsNullOrWhiteSpace(name))
			{
				submitButton.Attributes["name"] = name;
			}

			submitButton.Attributes["type"] = "submit";

			if (!string.IsNullOrEmpty(text))
			{
				submitButton.Attributes["value"] = text;
			}

			return MvcHtmlString.Create(submitButton.ToString(TagRenderMode.SelfClosing));
		}
	}
}